﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using Helpers;
using System.ComponentModel.Composition;
using DataModel;
using System.Windows.Controls;
using System.Windows.Input;
using Barcode;

namespace Shelfmanager
{
    [Export(typeof(IModule))]
    [ModuleMetadata(Name = "Collecting books", Short = "BookCollect", Group = "Shelf", IconFilename = "BookCollect.png")]
    public class BookCollectorViewModel : NotifyPropertyChangedBase, IModule
    {
        [Import(typeof(BookViewModel))]
        BookViewModel _book;

        public BookViewModel Book
        {
            get { return _book; }
            set
            {
                _book = value;
                NotifyPropertyChanged("Book");
            }
        }

        RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(param => this.Save(),
                        param => this.CanSave);
                }
                return _saveCommand;
            }
        }

        RelayCommand _addBookCommand;
        public ICommand AddBookCommand
        {
            get
            {
                if (_addBookCommand == null)
                {
                    _addBookCommand = new RelayCommand(param => this.AddBook(),
                        param => this.CanAddBook);
                }
                return _addBookCommand;
            }
        }

        public void AddBook()
        {
            Book = new BookViewModel();
        }

        public bool CanAddBook { get { return true; } }

        public void Save()
        {
            DataLayer data = DataLayer.Data;
            Book b = new Book
            {
                ISBN = _book.ISBN,
                ASIN = _book.ASIN,
                Title = _book.Title,
                Authors = _book.Authors.ToList(),
                Publisher = _book.AddPublisher(_book.Publisher),
                Binding = _book.Binding,
                LanguageOriginal = _book.LanguageOriginal,
                LanguagePublished = _book.LanguagePublished,
                NumberOfPages = _book.NumberOfPages,
                PublicationDate = _book.PublicationDate,
                Subject = _book.Subject,
                Class = _book.Class,
                DetailPageUrl = _book.DetailPageUrl
            };
            
            foreach (Author a in b.Authors)
                Book.AddAuthor(a.Name);
            Book.AddPublisher(b.Publisher.Name);

            data.Books.Add(b);
            try
            {
                data.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger.Write("SaveChanges(): Exception " + ex.ToString());
            }
        }

        public bool CanSave { get { return true; } }

        public UserControl View()
        {
            Logger.Write("BookCollector.ViewModel.View()");

            var bvc = new BookCollectorView();
            bvc.DataContext = this;
            BarcodeReader br = new BarcodeReader();
            br.BarcodeEvent += new EventHandler<BarcodeEventArgs>(br_BarcodeEvent);
            br.Start(bvc);
            return bvc;
        }

        void br_BarcodeEvent(object sender, BarcodeEventArgs e)
        {
            Book.ISBN = e.Data;
        }
    }
}
